package com.example.twoDataSource.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

//@Configuration
public class DataSourceConfig
{
    
    @Autowired
    private MysqlDataSourceConfig mysqlDataSourceConfig;
    
    @Autowired
    private MysqlDataSourceConfig2 mysqlDataSourceConfig2;
    
    @Bean (name = "db1")
    public DataSource dataSource1()
    {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(mysqlDataSourceConfig.getUrl());
        datasource.setUsername(mysqlDataSourceConfig.getUsername());
        datasource.setPassword(mysqlDataSourceConfig.getPassword());
        datasource.setDriverClassName(mysqlDataSourceConfig.getDriverClassName());

        //configuration
        datasource.setInitialSize(mysqlDataSourceConfig.getInitialSize());
        datasource.setMinIdle(mysqlDataSourceConfig.getMinIdle());
        datasource.setMaxActive(mysqlDataSourceConfig.getMaxActive());
        datasource.setMaxWait(mysqlDataSourceConfig.getMaxActive());
        datasource.setTimeBetweenEvictionRunsMillis(mysqlDataSourceConfig.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(mysqlDataSourceConfig.getMinEvictableIdleTimeMillis());
        datasource.setValidationQuery(mysqlDataSourceConfig.getValidationQuery());
        datasource.setTestWhileIdle(mysqlDataSourceConfig.isTestWhileIdle());
        datasource.setTestOnBorrow(mysqlDataSourceConfig.isTestOnBorrow());
        datasource.setTestOnReturn(mysqlDataSourceConfig.isTestOnReturn());
        try {
            datasource.setFilters(mysqlDataSourceConfig.getFilters());
        } catch (SQLException e) {
            System.err.println("druid configuration initialization filter: " + e);
        }
        datasource.setConnectionProperties(mysqlDataSourceConfig.getConnectionProperties());
        return datasource;
    }
    
    @Bean (name = "db2")
    public DataSource dataSource2()
    {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(mysqlDataSourceConfig2.getUrl());
        datasource.setUsername(mysqlDataSourceConfig2.getUsername());
        datasource.setPassword(mysqlDataSourceConfig2.getPassword());
        datasource.setDriverClassName(mysqlDataSourceConfig2.getDriverClassName());

        //configuration
        datasource.setInitialSize(mysqlDataSourceConfig2.getInitialSize());
        datasource.setMinIdle(mysqlDataSourceConfig2.getMinIdle());
        datasource.setMaxActive(mysqlDataSourceConfig2.getMaxActive());
        datasource.setMaxWait(mysqlDataSourceConfig2.getMaxActive());
        datasource.setTimeBetweenEvictionRunsMillis(mysqlDataSourceConfig2.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(mysqlDataSourceConfig2.getMinEvictableIdleTimeMillis());
        datasource.setValidationQuery(mysqlDataSourceConfig2.getValidationQuery());
        datasource.setTestWhileIdle(mysqlDataSourceConfig2.isTestWhileIdle());
        datasource.setTestOnBorrow(mysqlDataSourceConfig2.isTestOnBorrow());
        datasource.setTestOnReturn(mysqlDataSourceConfig2.isTestOnReturn());
        try {
            datasource.setFilters(mysqlDataSourceConfig2.getFilters());
        } catch (SQLException e) {
            System.err.println("druid configuration initialization filter: " + e);
        }
        datasource.setConnectionProperties(mysqlDataSourceConfig2.getConnectionProperties());
        return datasource;
    }
    
    /**
     * 动态数据源: 通过AOP在不同数据源之间动态切换
     * @return
     */
    @Primary
    @Bean (name = "dynamicDS1")
    @DependsOn ({"db1", "db2" })
    public DataSource dataSource()
    {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 默认数据源
        dynamicDataSource.setDefaultTargetDataSource(dataSource1());
        
        // 配置多数据源
        Map<Object, Object> dsMap = new HashMap<Object, Object>();
        dsMap.put("db1", dataSource1());
        dsMap.put("db2", dataSource2());
        
        dynamicDataSource.setTargetDataSources(dsMap);
        
        return dynamicDataSource;
    }
}
